package com.unhz.taigu.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.unhz.generator.dao.SysUserMapper;
import com.unhz.generator.pojo.SysPermission;
import com.unhz.generator.pojo.SysUser;
import com.unhz.generator.utils.EmptyUtil;
import com.unhz.generator.utils.IsNumeric;
import com.unhz.generator.utils.Page;
import com.unhz.taigu.service.SysUserService;
import com.unhz.taigu.service.permission.SysPermissionCustomService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by cxg on 2017/11/8 0008.
 *
 * @Author 曹显贵
 * @Date 2017/11/8 0008 11:34
 */
@Service
public class SysUserServiceImpl implements SysUserService {
    static final int PAGENUM = 1;
    static final int DISABLE =1;
    static final int ENABLE= 0;
    private Logger log = LoggerFactory.getLogger(SysUserServiceImpl.class);

    @Autowired
    private SysPermissionCustomService sysPermissionCustomService;


    @Autowired
    private SysUserMapper sysUserMapper;

    public SysUser selectAll() {
        return sysUserMapper.selectByPrimaryKey(1);
    }

    public List<SysUser> findAllUser() {
        return sysUserMapper.findAllUser();
    }

    @Override
    public String insertUser(SysUser sysUser) {
        if(EmptyUtil.isEmpty(sysUser.getUserStatus())){
            sysUser.setUserStatus(DISABLE);
        }
        sysUser.setIsDelete("0");
        int count =  sysUserMapper.insertSelective(sysUser);
        if(count >0){
            return "success";
        }
        return "false";
    }

    @Override
    public SysUser selectByUserId(Integer userId) {
        return sysUserMapper.selectByPrimaryKey(userId);
    }



    public PageInfo<SysUser> selectByPage(Integer pageNo, Integer pageSize) {

        PageHelper.startPage(pageNo,pageSize);
        List<SysUser> sysUserList = sysUserMapper.findAllUser();
        PageInfo<SysUser> pageInfo = new PageInfo<SysUser>();
        pageInfo.setList(sysUserList);
        pageInfo.setPageNum(pageNo);
        pageInfo.setPageSize(pageSize);
        return pageInfo;
    }

    @Override
    public Page<SysUser> selectByOldPage(String pageNo, Integer pageSize, String keywords) {
        int pageNum = PAGENUM;
        boolean numok = IsNumeric.isNumeric(pageNo);
        if(numok){
            if(pageNo !=null && !"".equals(pageNo)){
                pageNum = Integer.parseInt((pageNo));
            }
        }
        int totalRecords = sysUserMapper.getTotalRecordsByKeywords(keywords);        //获取记录总条数
        Page page = new Page(pageNum,totalRecords);
        page.setPageSize(pageSize);
        Map<String ,Object> map = new HashMap<String,Object>();
        map.put("startIndex",page.getStartIndex());
        map.put("pageSize",page.getPageSize());
        map.put("keywords",keywords);
        //获取总记录数
        List<SysUser> records = sysUserMapper.findAllUserMap(map);
        page.setRecords(records);
        String url = "/userController/findAllUser?keywords="+keywords;
        page.setUri(url);
        return page;
    }

    /**
     * 执行删除
     * @param userId
     * @return
     */
    @Override
    public String deleteUserById(Integer userId) {
        int result = sysUserMapper.deleteUserById(userId);
        if (result>0){
            log.info("用户删除成功！");
            return "success";
        }else {
            log.info("用户删除失败！");
            return "faild";
        }
    }


    @Override
    public SysUser selectByUserName(String userCode) {
        return sysUserMapper.selectByUserName(userCode);
    }

    @Override
    public List<SysPermission> findMenuByUserId(Integer id) {
        List<SysPermission> list = sysPermissionCustomService.findMenuByUserId(id);
        if(!EmptyUtil.isEmpty(list)){
            return list;
        }else{

        }
        return null;
    }

    @Override
    public List<SysPermission> findPermissionByUserId(Integer id) {
        List<SysPermission> list = sysPermissionCustomService.findPermissionByUserId(id);
        if(!EmptyUtil.isEmpty(list)){
            return list;
        }else{

        }
        return null;
    }
    @Override
    public SysUser loginUser(String userPhone, String password) {
        return sysUserMapper.loginUser(userPhone,password);
    }

    @Override
    public int updateUserById(SysUser sysUser) {
        return sysUserMapper.updateByPrimaryKeySelective(sysUser);
    }

    @Override
    public SysUser selectByPrimaryKey(Integer id) {
        return sysUserMapper.selectByPrimaryKey(id);
    }

    @Override
    public String updateUserPassword(SysUser sysUser) {
        int count = sysUserMapper.updateByPrimaryKeySelective(sysUser);
        if(count > 0){
            return "success";
        }
        return "false";
    }

    @Override
    public Page<SysUser> findPageAllSysUser(String pageNo, Integer pageSize, String keywords) {
        int pageNum = PAGENUM;
        boolean numok = IsNumeric.isNumeric(pageNo);
        if(numok){
            if(pageNo !=null && !"".equals(pageNo)){
                pageNum = Integer.parseInt((pageNo));
            }
        }
        int totalRecords = sysUserMapper.getTotalRecordsByKeywords("%"+keywords+"%");        //获取记录总条数
        Page page = new Page(pageNum,totalRecords);
        page.setPageSize(pageSize);
        Map<String ,Object> map = new HashMap<String,Object>();
        map.put("startIndex",page.getStartIndex());
        map.put("pageSize",page.getPageSize());
        map.put("keywords","%"+keywords+"%");
        //获取总记录数
        List<SysUser> records = sysUserMapper.findAllUserMap(map);
        page.setRecords(records);
        String url = "/sysUserController/findPageAllSysUser?keywords="+keywords;
        page.setUri(url);
        return page;
    }
    //禁用修改用户状态
    @Override
    public String updateUserStatus(SysUser sysUser) {
        if(sysUser.getUserStatus()==ENABLE){
            sysUser.setUserStatus(DISABLE);
            log.info("禁用管理员成功");
        }else{
            sysUser.setUserStatus(ENABLE);
            log.info("启用管理员成功");
        }
        int count = sysUserMapper.updateByPrimaryKeySelective(sysUser);
        if(count > 0 ){
            return "success";
        }
        return "failed";
    }


}
